import 'package:flutter/material.dart';
import '../../model/post.dart';
import '../../demo/listview_demo.dart';
import '../../demo/hello_demo.dart';
import '../../demo/drawer_demo.dart';
import '../../demo/bottom_navigation_bar_demo.dart';
import '../../demo/basic_demo.dart';
import '../../demo/view_demo.dart';

class SettingsPage extends StatefulWidget {
  SettingsPage({Key key}) : super(key: key);

  @override
  _SettingsPageState createState() => _SettingsPageState();
}

// 1 实现 SingleTickerProviderStateMixin
class _SettingsPageState extends State<SettingsPage> with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  int _index = 0;

  // 2 定义 TabController 变量
  TabController _tabController;

  @override
  bool get wantKeepAlive => true;

  // 3 覆盖重写 initState，实例化 _tabController
  @override
  void initState() {
    print("_SettingsPageState::initState");
    super.initState();
    _tabController = new TabController(length: 4, vsync: this);
    _tabController.addListener(() {
      print(_tabController.index);
    });
  }

  @override
  Widget build(BuildContext context) {
    print("_SettingsPageState::build");

    return Scaffold(
      appBar: PreferredSize(
        child: AppBar(
          bottom: TabBar(
            controller: _tabController, // 4 需要配置 controller！！！
            // isScrollable: true,
            tabs: <Widget>[
              Tab(text: '推荐'),
              Tab(text: '最新'),
              Tab(text: '最新'),
              Tab(text: '最新'),
            ],
          ),
        ),
        preferredSize: Size.fromHeight(48),
      ),
      body: TabBarView(
        controller: _tabController, // 4 需要配置 controller！！！
        children: <Widget>[
          ListViewDemo(),
          BasicDemo(),
          Icon(
            Icons.directions_bike,
            size: 128,
            color: Colors.cyan,
          ),
          ViewDemo(),
        ],
      ),
    );
  }
}
